package com.dt.global.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

	private DateTimeUtil() {

	}

	/**
	 * 根据输入的年月返回这个月的第一天和最后一天
	 * 
	 * @param yearMonth
	 *            格式：2005-09
	 * @return String[0]:这个月的第一天(2005-09-01)；String[1]:这个月的最后一天(2005-09-31)
	 */
	public static String[] getFirstAndLastDate(String yearMonth) {
		String[] date = new String[2];

		String[] d = yearMonth.split("-");
		if (d.length != 2 || d[0].length() != 4 || d[1].length() != 2) {
			date = null;
		} else {
			Calendar cal = Calendar.getInstance();
			cal.set(Integer.parseInt(d[0]), Integer.parseInt(d[1]) - 1, 1);
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH);
			int firstDay = cal.getActualMinimum(Calendar.DATE);
			int lastDay = cal.getActualMaximum(Calendar.DATE);

			cal.set(year, month, firstDay, 0, 0, 0);
			Date begin = cal.getTime();
			cal.set(year, month, lastDay, 23, 59, 59);
			Date end = cal.getTime();

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String b_d = df.format(begin);
			String e_d = df.format(end);
			date[0] = b_d;
			date[1] = e_d;
		}
		return date;
	}
	
	/**
	 * 取得当前年月
	 * 
	 * @return 2004-04
	 */
	public static String getTodayYearMonth() {
		SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM");
		return simpledateformat.format(new java.util.Date());
	}

	/**
	 * 取得今天日期
	 * 
	 * @return 2007-04-04
	 */
	public static String getToday() {
		SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
		return simpledateformat.format(new java.util.Date());
	}
	
}
